package com.kfm.base.collection;

import com.kfm.base.movie.Student;

import java.util.Iterator;
import java.util.Vector;

/**
 * 创建一个集合， 存储 5 个 Student 对象(name, age, toString, equals, hashCode, getter/setter).
 * 1. 设计方法对集合元素，按照年龄进行排序
 * 2. 输出排序后的集合元素
 * 3. 将下标为 3 的学生的姓名改成 "三娃"
 * 4. 在下标为 4 处添加学生 五娃 20岁
 * 6. 删除最后一个学生
 * 7. 遍历输出每个学生的姓名
 */
public class Exer {

    public static void main(String[] args) {
        Vector vector = new Vector();

        Student student = new Student("张三", 20);
        Student student1 = new Student("张三1", 19);
        Student student2 = new Student("张三2", 20);
        Student student3 = new Student("张三3", 21);
        Student student4 = new Student("张三4", 10);

        vector.add(student);
        vector.add(student1);
        vector.add(student2);
        vector.add(student3);
        vector.add(student4);


        System.out.println(vector);

        // 用比较器对集合进行排序
//        vector.sort(new Comparator() {
//            @Override
//            public int compare(Object o1, Object o2) {
//                if (o1 instanceof Student stu1 && o2 instanceof Student stu2) {
//                    return stu1.getAge() - stu2.getAge();
//                }
//                return 0;
//            }
//        });

        vector.sort((o1, o2) -> {
            if (o1 instanceof Student stu1 && o2 instanceof Student stu2) {
                    return stu1.getAge() - stu2.getAge();
            }
            return 0;
        });



//        System.out.println(vector);

        for (Object obj : vector) {
            System.out.println(obj);
        }

        Student o = (Student)vector.get(3);
        o.setName("三娃");

        System.out.println(vector.get(3));

        vector.add(4, new Student("五娃", 20));

        System.out.println(vector);

        vector.remove(vector.size() - 1);

        System.out.println(vector);

        // 迭代器
        Iterator iterator = vector.iterator();

        while (iterator.hasNext()){
            Object obj = iterator.next();
            if (obj instanceof Student stu){
                System.out.println(stu.getName());
            }
        }
    }
}
